/**
 * Project Name:HomeActivity
 * Date:2016-3-21上午10:49:19
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.utils;

import java.util.ArrayList;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * ClassName: EMailUtils <br/>
 * date: 2016-3-21 上午10:49:19 <br/>
 * 
 * @author askzhao
 * @version
 */
public class EmailUtils {
	static class MyAuthenticator extends javax.mail.Authenticator {
		private String strUser;
		private String strPwd;

		public MyAuthenticator(String user, String password) {
			this.strUser = user;
			this.strPwd = password;
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(strUser, strPwd);
		}
	}

	/**
	 * send_mail_file:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author askzhao
	 * @param str_to_mail
	 * @param str_from_mail
	 * @param str_smtp
	 * @param str_user
	 * @param str_pass
	 * @param str_title
	 * @param str_body
	 * @param paths
	 */
	public static void send_mail_file(String str_to_mail, String str_from_mail,
			String str_smtp, String str_user, String str_pass,
			String str_title, String str_body, ArrayList<String> paths) {

		String host = str_smtp; // 发件人使用发邮件的电子信箱服务器
		String from = str_from_mail; // 发邮件的出发地（发件人的信箱）
		String to = str_to_mail; // 发邮件的目的地（收件人信箱）

		Properties props = System.getProperties();// Get system properties

		props.put("mail.smtp.host", host);// Setup mail server

		props.put("mail.smtp.auth", "true"); // 这样才能通过验证

		MyAuthenticator myauth = new MyAuthenticator(str_user, str_pass);// Get
																			// session

		Session session = Session.getDefaultInstance(props, myauth);

		MimeMessage message = new MimeMessage(session); // Define message

		try {
			message.setFrom(new InternetAddress(from)); // Set the from address
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		try {
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));// Set the to address
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		try {
			message.setSubject(str_title);// Set the subject
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		MimeMultipart allMultipart = new MimeMultipart("mixed"); // 附件
		if (paths != null && paths.size() > 0) {

			for (String path : paths) {
				MimeBodyPart attachPart = new MimeBodyPart();
				FileDataSource fds = new FileDataSource(path); // 打开要发送的文件
				try {
					attachPart.setDataHandler(new DataHandler(fds));
				} catch (MessagingException e) {
					e.printStackTrace();
				}
				try {
					attachPart.setFileName(fds.getName());
				} catch (MessagingException e) {
					e.printStackTrace();
				}
				try {
					allMultipart.addBodyPart(attachPart);// 添加
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}

			
		}
		try {
			MimeBodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(str_body, "text/plain;charset=utf-8");
			allMultipart.addBodyPart(messageBodyPart);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			message.setContent(allMultipart);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		sent(message);

	}

	public static void sent(final MimeMessage message) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					message.saveChanges();
				} catch (MessagingException e) {
					e.printStackTrace();
				}

				try {
					Transport.send(message);// 开始发送
				} catch (MessagingException e) {
					e.printStackTrace();
				}

			}
		}).start();
	}

	/**
	 * sendEmail:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author askzhao
	 * @param to
	 * @param string
	 * @param paths
	 */
	public static void sendEmail(String title, String body,
			ArrayList<String> paths) {
		send_mail_file("feedback@kuaikuaidai.com", "client@kuaikuaidai.com",
				"smtp.exmail.qq.com", "client@kuaikuaidai.com", "Kk2016",
				title, body, paths);

	}

}